#include <netinet/in.h>
#include <sys/socket.h>		/* socket(), bind(), listen(), accept() */
#include <sys/types.h>		/* see man 2 socket */
#include <strings.h>		/* bzero() */
#include <arpa/inet.h>		/* htons(), htonl() */
#include <time.h>			/* time(), ctime() */
#include <stdio.h>			/* snprintf() */
#include <unistd.h>			/* write() */
#include <string.h>			/* strlen() */

#define	MAXLINE		4096	/* max text line length */
#define LISTENQ		100		/* will use in listen()'s backlog argument */

int
main(int argc, char *argv[])
{
	int					listenfd, connfd;
	struct sockaddr_in	servaddr;
	time_t				timeUTC;
	char				buff[MAXLINE];	/* store human-readable string */
	
	listenfd = socket(AF_INET, SOCK_STREAM, 0);

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(13);
	servaddr.sin_addr.s_addr = htonl(INADDR_ANY);	/* s_addr */

	bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr) );

	/* convert socket to listening socket */
	listen(listenfd, LISTENQ);

	for ( ; ; ) {
		connfd = accept(listenfd, (struct sockaddr *)NULL, NULL);

		timeUTC = time(NULL);
		snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&timeUTC));
		write(connfd, buff, strlen(buff));		/* not sizeof(buff) */

		close(connfd);
	}
}
